/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.multishop.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.dto.AreaDto;
import com.yami.shop.bean.model.Area;
import com.yami.shop.bean.vo.AreaCodeVo;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.service.AreaService;
import com.yami.shop.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lgh on 2018/10/26.
 */
@RestController
@RequestMapping("/admin/area")
@Api(tags="地区信息接口")
public class AreaController {

    @Autowired
    private AreaService areaService;

    @Autowired
    private SysConfigService sysConfigService;

    @GetMapping("/list")
    @ApiOperation(value = "获取省市区地区信息列表（可传入地区名称搜索）", notes = "分页获取省市区地区信息列表（可传入地区名称搜索）")
    public ServerResponseEntity<List<Area>> list(Area area) {
        List<Area> areas = areaService.list(new LambdaQueryWrapper<Area>()
                .like(area.getAreaName() != null, Area::getAreaName, area.getAreaName()));
        return ServerResponseEntity.success(areas);
    }

    @GetMapping("/listByPid")
    @ApiOperation(value = "通过父级id获取区域列表", notes = "通过父级id获取区域列表")
    public ServerResponseEntity<List<Area>> listByPid(Long pid) {
        List<Area> list = areaService.listByPid(pid);
        return ServerResponseEntity.success(list);
    }

    @GetMapping("/areaListInfo")
    @ApiOperation(value = "获取省市区信息", notes = "获取省市区信息")
    public ServerResponseEntity<List<AreaDto>> getAreaListInfo() {
        return ServerResponseEntity.success(areaService.getAreaListInfo());
    }

    @GetMapping("/listAreaOfEnable")
    @ApiOperation(value = "获取可用的省市区列表(完整路径）", notes = "获取可用的省市区列表（完整路径）")
    public ServerResponseEntity<List<AreaDto>> listAreaOfEnable() {
        List<AreaDto> list = areaService.listAreaOfEnable();
        return ServerResponseEntity.success(list);
    }
    @GetMapping("/getAreaCodeList")
    @ApiOperation(value = "获取国际区号信息列表", notes = "获取可用的省市区列表（完整路径）")
    public ServerResponseEntity<List<AreaCodeVo>> getAreaCodeList(){
        String phone_area_code = sysConfigService.getValue("PHONE_AREA_CODE");
        if(StringUtils.isNotBlank(phone_area_code)){
            List<AreaCodeVo> list = JSONObject.parseArray(phone_area_code, AreaCodeVo.class);
            return ServerResponseEntity.success(list);
        }
        return ServerResponseEntity.success(null);
    }
}
